package com.gy.boot.common.util;

import com.gy.boot.common.exception.SysException;
import com.gy.boot.common.dao.IEntity;
import com.gy.boot.common.exception.SysException;

import java.util.Map;

/**
 * 自定义全局返回结果类
 * @author geYang
 */
public class R extends IEntity {

    public R set(String key, Object value) {
        super.set(key, value);
        return this;
    }

    public R setAll(Map<String, Object> map) {
        super.setAll(map);
        return this;
    }

    private final static int CODE_SUCCESS = 200;
    private final static int CODE_ERROR = 500;

    private R() {}

    private R(boolean b) {
        int code = b ? CODE_SUCCESS : CODE_ERROR;
        this.set("code", code).set("success", b);
    }

    public static R ok() {
        return new R(true);
    }
    
    public static R ok(Object data) {
        return R.ok().set("data", data);
    }

    public static R ok(Map<String, Object> map) {
        return R.ok().setAll(map);
    }
    
    private static R err() {
        return new R(false);
    }
    
    public static R err(String msg) {
        return R.err().set("msg", msg);
    }
    
    public static R err(int code, String msg) {
        return R.err(msg).set("code", code);
    }
    
    public static R err(SysException e) {
        return R.err(e.getCode(), e.getMessage());
    }

    public static R err(REnum r) {
        return R.err(r.getMsg());
    }

}
